package com.parking.carsystem.parkingchargesystem.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.parking.carsystem.parkingchargesystem.R;
import com.parking.carsystem.parkingchargesystem.activity.ChooseLisencePlateActivity;
import com.parking.carsystem.parkingchargesystem.activity.SelfServicePaymentActivity;
import com.parking.carsystem.parkingchargesystem.common.Constant;
import com.parking.carsystem.parkingchargesystem.listener.OnclickItemPlateNumber;
import com.parking.carsystem.parkingchargesystem.utils.SPUtils;
import com.parking.carsystem.parkingchargesystem.view.LicensePlateView;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import ttyy.com.recyclerexts.base.EXTRecyclerAdapter;
import ttyy.com.recyclerexts.base.EXTViewHolder;

public class InputLisensePlateFragment extends Fragment implements LicensePlateView.InputListener, View.OnClickListener {
    @BindView(R.id.activity_lpv)
    LicensePlateView activityLpv;
    Unbinder unbinder;
    @BindView(R.id.main_rl_container)
    RelativeLayout mainRlContainer;
    @BindView(R.id.hisrory_parking_data)
    RecyclerView hisrory_parking_data;
    @BindView(R.id.search_paking)
    TextView search_paking;
    @BindView(R.id.delete_history)
    ImageView delete_history;
    private EXTRecyclerAdapter<String> recyclerAdapter;
    private ArrayList<String> list;
    private String content;
    private String parkingHistoryList;
    private String[] split;
    private ChooseLisencePlateActivity activity;
    private String payment;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_choose_input, container, false);
        unbinder = ButterKnife.bind(this, inflate);
        initView();
        return inflate;
    }

    private void initView() {
        activity = (ChooseLisencePlateActivity) getActivity();
        if (list == null)
            list = new ArrayList<>();
        list.clear();
        parkingHistoryList = SPUtils.getInstance(getActivity()).getString(Constant.PARKINGHISTORY);
        if (!TextUtils.isEmpty(parkingHistoryList)) {
            split = parkingHistoryList.split("@");
            for (String s : split) {
                list.add(s);
            }
        }
        activityLpv.setInputListener(this);
        search_paking.setOnClickListener(this);
        delete_history.setOnClickListener(this);
        activityLpv.setKeyboardContainerLayout(mainRlContainer);
        activityLpv.showLastView();
        activityLpv.hideLastView();
        activityLpv.onSetTextColor(R.color.colorAccent);
        hisrory_parking_data.setLayoutManager(new GridLayoutManager(getActivity(), 4));
        recyclerAdapter = new EXTRecyclerAdapter<String>(R.layout.item_history_plate) {
            @Override
            public void onBindViewHolder(EXTViewHolder extViewHolder, int i, String s) {
                extViewHolder.setText(R.id.parking_name, s);
            }
        };
        recyclerAdapter.setDatas(list);
        hisrory_parking_data.setAdapter(recyclerAdapter);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void inputComplete(String content) {
        //获取到输入框的内容
        Toast.makeText(getActivity(), content, Toast.LENGTH_SHORT).show();
        this.content = content;
    }

    private void saveParkingPlate(String parkingName) {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            builder.append(list.get(i));
            if (i != list.size() - 1) {
                builder.append("@");
            }
        }
        SPUtils.getInstance(getActivity()).put(Constant.PARKINGHISTORY, builder.toString());
    }

    @Override
    public void deleteContent() {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.search_paking:
                if (!TextUtils.isEmpty(content)) {
                    //跳转到支付界面
                    if (TextUtils.isEmpty(payment)) {
                        list.add(content);
                        recyclerAdapter.notifyDataSetChanged();
                        saveParkingPlate(content);
                        Intent intent = new Intent(activity, SelfServicePaymentActivity.class);
                        intent.putExtra(Constant.PLATENUMBER, content);
                        startActivity(intent);
                    } else {
                        //关闭当前界面
                        Intent intent = new Intent();
                        intent.putExtra(Constant.PLATENUMBER, content);
                        activity.setResult(Constant.PLATENUMBERCODE, intent);
                        activity.finish();
                    }
                } else {
                    Toast.makeText(getActivity(), "请输入车牌号", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.delete_history:
                list.clear();
                SPUtils.getInstance(getActivity()).put(Constant.PARKINGHISTORY, "");
                recyclerAdapter.setDatas(null);
                recyclerAdapter.notifyDataSetChanged();
                break;
        }
    }

    public boolean getKeyDownVisible() {
        return activityLpv.getKeyDownVisible();
    }

    public void hidleOnkeyDown() {
        activityLpv.hildeKeyDown();
    }

    public void setPayMent(String payment) {
        this.payment = payment;
    }
}
